package com.example.helloserial;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.util.Log;

public class Uart extends ActionBarActivity {

	private static final String TAG = "HelloSerial";
	private FileInputStream mInputStream;
	private FileOutputStream mOutputStream;
	private FileDescriptor mFd;
	private String path = "/dev/ttyS2";
	
	private Button btnOpen=null;
	private Button btnRecv=null;
	private Button btnSend=null;
	private EditText mReception=null;	
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_uart);
		
		btnOpen = (Button)super.findViewById(R.id.open);
		btnOpen.setOnClickListener(new OpenUart());
		
		btnRecv = (Button)super.findViewById(R.id.recv);
		btnRecv.setOnClickListener(new RecvUart());
		
		mReception = (EditText) findViewById(R.id.EditTextEmission);
		
		btnSend = (Button)super.findViewById(R.id.send);
		btnSend.setOnClickListener(new SendOnClickListener());
	}

    void onDataReceived(final byte[] buffer, final int size) {
		runOnUiThread(new Runnable() {
			public void run() {
				if (mReception != null) {
					Log.d(TAG,"receive data:%02x"+buffer[0]);
					mReception.append(new String(buffer, 0, size));
				}
			}
		});
	}
	
	private class OpenUart implements OnClickListener {	
		private native FileDescriptor Uartopen(String path, int baudrate, int i, char c, int j);
		private File device = new File(path);
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try{
				System.loadLibrary("uart");
				Log.d(TAG,"libuart.so is loading!");
			}catch(Exception e) {
			      e.printStackTrace(System.out);
		    }
			mFd = Uartopen(device.getAbsolutePath(), 115200, 8, 'E', 1);
			if (mFd == null) {
				Log.d(TAG, "native open returns null");
			}
			else{
				Log.d(TAG,"-------------------uart open is ok!");
			}
		}
	}
	private class RecvUart implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int size;
			
			try {
			byte[] buffer = new byte[64];
			mInputStream=new FileInputStream(new File("/dev/ttyS2"));

			if (mInputStream == null){
				Log.d(TAG,"mInputStream is null!");
				return;
			}
			size = mInputStream.read(buffer);
			//mInputStream.close();
			Log.d(TAG,"Receive uart data size:"+size);
			if (size > 0) {
				onDataReceived(buffer, size);
				}
			} catch (IOException e){
				e.printStackTrace();
				return;
			}
		}
	}

	private class SendOnClickListener implements OnClickListener {
		private String sData="Hello serial!";
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				mOutputStream = new FileOutputStream(new File("/dev/ttyS2"));
				
				mOutputStream.write(sData.getBytes());
				mOutputStream.write('\n');
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
